/*
 * 企业列表
 * @Author: along
 * @Date: 2019-12-09 11:00:00
 */
<template>
    <div class="container companyVideo">
        <!-- header -->
        <div class="header">
            <div class="header-box">
                <p class="header-box-title">显示状态:</p>
                <el-select
                    v-model="header.status"
                    filterable
                    placeholder="请选择"
                    size="small"
                    clearable
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in statusList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="companyVideo"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="course_name"
                label="课程名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="course_sort_company"
                label="排序"
                :width="120"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.course_sort_company }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="course_show"
                label="展示状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.course_show == 1">已显示</span>
                    <span v-if="scope.row.course_show == 0">已隐藏</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        :disabled="scope.row.course_show == 1"
                        @click="fnSetStatus(scope.row)"
                    >
                        显示
                    </el-button>
                    <el-button
                        type="text"
                        :disabled="scope.row.course_show == 0"
                        @click="fnSetStatus(scope.row)"
                    >
                        隐藏
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 无数据页 -->
        <!-- <div
            class="empty-wrap"
            v-if="tableData.length == 0"
        >
            <div class="empty-img" />
            <p class="empty-text">
                暂时还没有数据哦~
            </p>
        </div> -->
        <!-- /无数据页 -->

        <!-- /分页 -->
        <!-- <div
            class="pagination-wrapper clearfix"
            v-if="tableData.length > 0"
        >
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div> -->
    </div>
</template>

<script>
export default {
    /**
     * @param  {Object}    authResult           //表单权限
     * @param  {Array}     tableData            //表单数据
     * @param  {Object}    page                 //表单分页
     * @param  {Object}    header               //表单检索字段
     * @param  {Array}     statusList           //公司名称列表
     * @param  {String}    pathName             //上一页路由名称
     * @param  {String}    prevPage             //上一页分页页码
     * @param  {String}    hire_no              //记录公司编号
     */
    data () {
        return {
            hire_no: '',
            prevPage: '',
            pathName: '',
            statusList: [{
                value: '',
                label: '全部'
            },{
                value: 1,
                label: '已显示'
            },{
                value: 0,
                label: '已隐藏'
            }],
            header: {
                status: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    beforeRouteEnter(to, from, next) {
        next(vm => {
            vm.pathName = from.name;
        });
    },
    mounted () {
        this.authResult = this.$route.meta.authResult;
        this.fnBreadcrumb();
        this.fnRecord();
        this.fnParamUrl();
        this.getTableData();
    },
    destroyed(){
        window.removeEventListener('popstate', this.fnGoBack, false);
    },
    methods: {
        /**
         * 设置表单header
         */
        fnBreadcrumb () {
            this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '工作'},{title: "企业视频"}]);
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/company_video%5Cget_list', {
                hire_no: this.hire_no,
                course_show: this.header.status
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                }
            });
        },

        /**
         * 排序
         */
        fnEditSort (row) {
            this.$prompt('请输入排序号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '排序号格式不正确,请重新输入'
            }).then(({ value }) => {
                this.$post('/student/company_video%5Cmodify',{
                    course_no: row.course_no,
                    course_sort_company: value,
                    course_show: row.course_show
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData(this.page.now_page);
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 设置状态
         */
        fnSetStatus (row) {
            this.$post('/student/company_video%5Cmodify',{
                course_no: row.course_no,
                course_sort_company: row.course_sort_company,
                course_show: row.course_show == 1 ? 0 : 1
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData(this.page.now_page);
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 记录上一页路由,监听页面返回并重定向
         */
        fnRecord () {
            if (window.history && window.history.pushState) {
                history.pushState(null, null, document.URL);
                window.addEventListener('popstate', this.fnGoBack, false);
            }
        },

        /**
         * 获取上一页数据
         */
        fnParamUrl () {
            let query = this.$route.query;

            if (query.page !== undefined || query.hire_no !== undefined) {
                this.prevPage = query.page;
                this.hire_no = query.hire_no;
            }
        },

        /**
         * 上一页重定向
         */
        fnGoBack () {
            this.$router.replace({
                name: this.pathName,
                query: {
                    page: this.prevPage
                }
            });
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            .header-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    /* 无数据 */
    .empty-wrap {
        text-align: center;
        margin-top: 150px;
        .empty-img {
            width: 400px;
            height: 300px;
            margin: 0 auto;
            background-image: url(//cdn.zsdx.cn/wei/images/hire/manage/market/empty-good-list.png);
            background-position: center;
            background-size: contain;
            background-repeat: no-repeat;
        }
        .empty-text {
            text-align: center;
            margin-top: 40px;
        }
    }
</style>
<style lang="less">
    .companyVideo .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .companyVideo .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .companyVideo .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .companyVideo .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
</style>